<template>
  <div>
    <div v-show="$route.name == 'store-back-list'">
      <!-- 面包屑导航区域 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>退货单列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片视图区域 -->
      <el-card>
        <div style="margin-bottom: 20px;">退货单列表</div>
        <!-- 搜索与添加区域 -->
        <el-row class="query">
          <el-col :span="24">
            <el-button type="success" @click="addBack" style="margin-bottom: 10px">添加退货单</el-button>
          </el-col>
        </el-row>
        <el-row class="query">
          <el-col :span="24">
            <el-date-picker
              v-model="searchList.backTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
            <el-button style="margin-left: 10px"  @click="reset">重置</el-button>
            <el-button type="primary" @click="search()" style="margin-left: 10px;">搜索</el-button>
            <el-button type="warning" @click="exportExcel()" style="margin-left: 10px;">导出至excel</el-button>
          </el-col>
        </el-row>
        <el-table
        border
        v-loading="loading"
        :data="storeBackData"
        style="margin-top: 10px;">
        <el-table-column label="申请日期" prop="back_time" min-width="110" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.back_time ? new Date(scope.row.back_time).toLocaleDateString().split('/').join('-') : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退货单号" prop="apply_number" min-width="110" align="center"></el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="clickButton(scope.row.id)" style="margin-right: 5px;">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>

      </el-card>
    </div>
    <div>
      <router-view />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      searchList: {
        backTime: ''
      },
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      storeBackData: []

    }
  },
  mounted() {
    this.getBackList()
  },
  watch: {
    $route(now, old) {
      if (old.name === 'store-back-save' || old.name === 'store-back-detail') {
        this.getBackList()
      }
    }
  },
  methods: {
    addBack() {
      this.$router.push({ 'name': 'store-back-save' })
    },
    async getBackList() {
      this.loading = true
      var params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize,
      }
      if (this.searchList.backTime) {
        const startTime = new Date(this.searchList.backTime[0])
        const syear = startTime.getFullYear() // 获取当前年份
        const smonth = startTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
        const sday = startTime.getDate() // 获取当前日（1-31）
        const endTime = new Date(this.searchList.backTime[1])
        const eyear = endTime.getFullYear() // 获取当前年份
        const emonth = endTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
        const eday = endTime.getDate() // 获取当前日（1-31）
        if (startTime) {
          params.backStart = `${syear}-${smonth}-${sday}`
        }
        if (endTime) {
          params.backEnd = `${eyear}-${emonth}-${eday}`
        }
      }
      const { data: result } = await this.$http.get('/store-back', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.storeBackData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.storeBackData = result.data
      this.pager = result.pager
    },
    clickButton(id) {
      this.$router.push({
        name: 'store-back-detail',
        params: { back_id: id}
      })
    },
    reset() {
      this.searchList = {
        backTime: ''
      },
      this.storeBackData = []
      this.pager.currPage = 1
      this.getBackList()
    },
    search() {
      this.storeBackData = []
      this.pager.currPage = 1
      this.getBackList()
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getBackList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getBackList()
    },
    async exportExcel() {
      this.loading = true
      var data = {}
      if (this.searchList.backTime) {
        const startTime = new Date(this.searchList.backTime[0])
        const syear = startTime.getFullYear() // 获取当前年份
        const smonth = startTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
        const sday = startTime.getDate() // 获取当前日（1-31）
        const endTime = new Date(this.searchList.backTime[1])
        const eyear = endTime.getFullYear() // 获取当前年份
        const emonth = endTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
        const eday = endTime.getDate() // 获取当前日（1-31）
        if (startTime) {
          data.backStart = `${syear}-${smonth}-${sday}`
        }
        if (endTime) {
          data.backEnd = `${eyear}-${emonth}-${eday}`
        }
      }
      await this.$http.post('/store-back/export', data, { type: 'json',responseType:'blob' }).then(res => {
        if (res.status === 200) {
          const blob = res.data
          const reader = new FileReader()
          reader.readAsDataURL(blob)
          reader.onload = (e) => {
            const a = document.createElement('a')
            a.download = '冠记-退货明细.xlsx'
            a.href = e.target.result
            a.click()
          }
          this.loading = false
          return this.$message.success('导出成功')
        }
        this.loading = false
        return this.$message.error(res.msg)
      }).catch (() => {
        this.loading = false
        return this.$message.error('导出失败')
      })
    }
  }
}
</script>

